﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;
using Microsoft.SharePoint;

namespace WoltersKluwer.SPChainGang.Core.Constants
{
    public static class GlobalConstants
    {
        public const string ReportFolderNamePattern = "Report for the scan '{0}' started in mode '{1}' on {2}";
        public const string LogEntryTemplate = "[{0}] SPChainGang job {1}: {2}";
        public const string LogExceptionTemplate = "[{0}] SPChainGang exception in job {1}: {2} ({3})";
        public const string ChainGangJobNameTemplate = "SPChainGangJob (created on {0})";
        public const string ImmediateRunJobNameTemplate = "Immediate Scan for {0}";
        public const int ConfigurationListTemplateId = 10101;
        public const string ReportsListName = "SPChainGang Links";
        public const string ReportsLibraryName = "SPChainGang Reports";
        public static readonly Guid SPChainGangListTemplatesFeatureId = new Guid("{B1496B37-046E-4A57-8C07-21F1CCB03AB4}");
        public static readonly SPQuery EmptyQuery = new SPQuery { Query = "0" };
        public const string ReportAdditionalInfoFieldPattern = "Field: {0} ({1})";
        public const string ReportAdditionalInfoWebPartPattern = "{0} ({1})";


        private static readonly string cultureSpecificPattern = String.Format("{0} {1}",
                                                                              CultureInfo.CurrentCulture.DateTimeFormat.
                                                                                  ShortDatePattern,
                                                                              CultureInfo.CurrentCulture.DateTimeFormat.
                                                                                  LongTimePattern);

        public static readonly string TimeStampPattern =
            cultureSpecificPattern.Insert(cultureSpecificPattern.LastIndexOf("ss") + 2, ".fff");

        public static readonly Regex UriMapper = //for html
            new Regex( //TODO: add iframes and other HTML elements having URI (asp and sharepoint tags maybe)
                       //and then remove quotes capturing from the one below
                @"(?:(?'a'<a)|<img)\s+.*?(?(a)href|src)\s*=\s*(?:""(?<url>[^""]*)""|'(?<url>[^']*)'|(?<url>[^\s>]+)).*?>",
                RegexOptions.IgnoreCase | RegexOptions.Compiled);

        public static readonly Regex UrlMapper = //for plain text and in some cases ASPX markup
            new Regex(
                @"(?:(?'dblquote'="")|(?'quote'='))?(?<url>https?://(?(dblquote)[^""]*|(?(quote)[^']*|([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\,]*)?)))",
                RegexOptions.IgnoreCase | RegexOptions.Compiled);
    }
}
